/**
 * 
 */
package com.cqan.product.controller;

import static com.cqan.common.ViewName.*;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.orm.PropertyFilter;
import com.cqan.product.entity.Product;
import com.cqan.product.entity.Product.ProductType;
import com.cqan.product.service.ProductService;
import com.google.common.collect.Lists;

/**
 * @Description TODO
 * @author slaton.wu@gmail.com
 * @date 2012-7-5下午3:15:59
 *
 */
@Controller("productController")
@RequestMapping("/product")
public class ProductController extends BaseController<Product> {
	
	@Resource(name="productService")
	private ProductService productService;
	
	/**
	 * 产品管理列表
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.htm")
	public ModelAndView list(Integer pageNo,HttpServletRequest request){
		ModelAndView mav = new ModelAndView(PRODUCT_LIST);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page.setPageNo(pageNo);
		page = productService.search(page, filters);
		mav.addObject("page", page);
		return mav;
	}
	
	/**
	 * 删除产品
	 * @param pageNo
	 * @param request
	 * @return 
	 * @return
	 * @throws AppException 
	 * @throws IOException 
	 */
	@RequestMapping(value="/delete.htm",method=RequestMethod.POST)
	public String delete(String ids) throws AppException, IOException{
		try {
			if (ids!=null) {
				StringTokenizer st = new StringTokenizer(ids,"-");
				List<Long> list = Lists.newArrayList();
				while (st.hasMoreTokens()) {
					String id = st.nextToken();
					if (id!=null&&!"".equals(id)) {
						list.add(Long.parseLong(id));
					}
				}
				productService.delete(list);
				return "删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "该权限已有角色正在使用，暂不能删除！";
		}
		return  "删除失败！";
	}
	
	/**
	 * 添加产品
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.htm")
	public ModelAndView edit(Long id,int pageNo){
		ModelAndView mav = new ModelAndView(PRODUCT_ADD);
		if(id!=null&&id>0){
			Product product = this.productService.get(id);
			mav.addObject("pageNo", pageNo);
			mav.addObject("product", product);
		}
		return mav;
	}
	
	/**
	 * 保存产品
	 * @param request
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="/save.htm",method=RequestMethod.POST)
	public ModelAndView save(Long id, HttpServletRequest request) throws AppException{
		ModelAndView  mav = new ModelAndView(new RedirectView("list.htm?pageNo="+request.getParameter("pageNo")));
		try {
			Product product = this.getRequestEntity(request);
			String ptype = request.getParameter("productType");
			ProductType productType = Enum.valueOf(ProductType.class,ptype);
			product.setProductType(productType);
			if(id!=null){
				Product p = this.productService.get(id);
				product.setCreateTime(Calendar.getInstance().getTime());
				this.copyProperties(p, product);
				this.productService.update(p);
				return mav;
			}
			product.setCreateTime(Calendar.getInstance().getTime());
			this.productService.add(product);
		} catch (Exception e) {
			e.printStackTrace();
			throw new AppException();
		}	
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="/checkName.htm")
	public boolean  checkName(String name,Long id)throws AppException{	
		if(id==null){
			Product p = this.productService.findBy("name", name);
			if (p!=null) {
				return false;
			}
		}
		return true;
	}
}
